package com.sojson.util.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.shardingsphere.sharding.spring.boot.rule.YamlShardingRuleSpringBootConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.rule.YamlTableRuleConfiguration;

import com.sojson.util.SpringUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.db.bean.ShardingJdbcVo;

/**
 * Sharding-Jdbc工具类
 *
 * @author liu
 * @date 2024-05-20
 */
public class ShardingJdbcUtil {

    private static YamlShardingRuleSpringBootConfiguration shardingRule =
        SpringUtil.getBeanByType(YamlShardingRuleSpringBootConfiguration.class);

    /**
     * 获取建表语句
     *
     * @return
     */
    public static List<String> getCreateTableSql() {
        List<String> sqls = new ArrayList<>();

        Map<String, ShardingJdbcVo> tables = getTables();
        Set<Entry<String, ShardingJdbcVo>> entrySet = tables.entrySet();
        for (Entry<String, ShardingJdbcVo> entry : entrySet) {
            String tableName = entry.getKey();
            List<String> tableNameNews = entry.getValue().getTableNames();

            for (String tableNameNew : tableNameNews) {
                sqls.add(String.format("CREATE TABLE IF NOT EXISTS %s LIKE %s;", tableNameNew, tableName));
            }
        }
        return sqls;
    }

    /**
     * 获取重命名表语句
     *
     * @return
     */
    public static List<String> getChangeTableSql() {
        List<String> sqls = new ArrayList<>();

        Map<String, ShardingJdbcVo> tables = getTables();
        Set<Entry<String, ShardingJdbcVo>> entrySet = tables.entrySet();
        for (Entry<String, ShardingJdbcVo> entry : entrySet) {
            String tableName = entry.getKey();

            sqls.add(String.format("RENAME TABLE %s TO %sbak;", tableName, tableName));
        }
        return sqls;
    }

    /**
     * 获取删表语句
     *
     * @return
     */
    public static List<String> getRemoveTableSql() {
        List<String> sqls = new ArrayList<>();

        Map<String, ShardingJdbcVo> tables = getTables();
        Set<Entry<String, ShardingJdbcVo>> entrySet = tables.entrySet();
        for (Entry<String, ShardingJdbcVo> entry : entrySet) {
            List<String> tableNameNews = entry.getValue().getTableNames();

            for (String tableNameNew : tableNameNews) {
                sqls.add(String.format("DROP TABLE IF EXISTS %s;", tableNameNew));
            }
        }
        return sqls;
    }

    /**
     * 获取初始化数据标志语句
     *
     * @return
     */
    public static List<String> getShardingTagSetNull() {
        List<String> sqls = new ArrayList<>();

        Map<String, ShardingJdbcVo> tables = getTables();
        Set<Entry<String, ShardingJdbcVo>> entrySet = tables.entrySet();
        for (Entry<String, ShardingJdbcVo> entry : entrySet) {
            String tableName = entry.getKey();

            sqls.add(String.format("UPDATE %s SET SHARDING_TAG = NULL;", tableName));
        }
        return sqls;
    }

    /**
     * 获取所有动态表名
     *
     * @return
     */
    public static Map<String, ShardingJdbcVo> getTables() {
        Map<String, ShardingJdbcVo> tableNames = new HashMap<>();

        String splitStr = "\\.\\.";
        String startStr = "{";
        String endStr = "}";
        String actualDataNodes = null;
        String substring = null;
        String[] split = null;
        int start = 0;
        int ent = 0;
        Map<String, YamlTableRuleConfiguration> tables = shardingRule.getSharding().getTables();
        Set<Entry<String, YamlTableRuleConfiguration>> entrySet = tables.entrySet();
        for (Entry<String, YamlTableRuleConfiguration> entry : entrySet) {
            String tableName = entry.getKey();
            ShardingJdbcVo vo = tableNames.get(tableName);
            if (vo == null) {
                vo = new ShardingJdbcVo();
                vo.setTableName(tableName);
                vo.setTableNames(new ArrayList<>());
                tableNames.put(tableName, vo);
            }
            List<String> list = vo.getTableNames();
            tableName += StringUtil.SEPARATOR;

            YamlTableRuleConfiguration value = entry.getValue();
            actualDataNodes = value.getActualDataNodes();
            substring =
                actualDataNodes.substring(actualDataNodes.indexOf(startStr) + 1, actualDataNodes.indexOf(endStr));
            split = substring.split(splitStr);
            start = Integer.parseInt(split[0]);
            ent = Integer.parseInt(split[1]);
            for (int i = start; i <= ent; i++) {
                list.add(tableName + i);
            }
            String shardingColumn = value.getTableStrategy().getStandard().getShardingColumn();
            vo.setShardingColumn(shardingColumn);
        }

        return tableNames;
    }

}